window.K=window.K||{"URL":{"defaultTemplates":""}};
window._jstemplates=window._jstemplates||{};
K=$.extend(K,{
		"BasicGrid":{
		"View":null,
		"template":{"name":"default","url":"template.html"},
		"installation":function(){
			K.BasicGrid.View=Backbone.View.extend({
			    "tagName":"div",
			    "className":"basicgrid-wrapper",
			    "template":_.template($("#_template>#t_basicgrid").text()),
			    "dataURL":false,
			    "data":false,
			    "grid":null,
			    "pager":false,
			    "events":{"onButtonClick":{"_dft":function(grid,$btn){}},
			    		  "onDataClick":{"_dft":function(grid,row){}},
			    		  "onChageSize":function(grid,size){},
			    		  "onPageTo":function(grid,page){}},
			    "wrapper":null,
			    "setGrid":function(grid){
			    	this.grid=$.extend({
			    		"title":"",
				    	"subtitle":"",
				    	"hasCheckbox":false,
				    	"buttons":[],
						"cols":[],
						"size":[10,20],
						"sizeText":"Entries:"
			    	},grid);
			    	var total=0;
			    	this.grid.cols.map(function(v){total+=v.width||1});
			    	for(var i=0;i<this.grid.cols.length;i++){
			    		var c=this.grid.cols[i],_w=c.width||1;
			    		c.w=(_w/total)*100;
			    	}
			    	return this;
			    },
			    "setData":function(data_url){
			    	if(typeof(data_url)=="string")this.dataURL=data_url;
			    	else{this.data=data_url;}
			    	return this;
			    },"setURL":function(url){
			    	this.dataURL=url;
			    	return this;
			    },
			    "addData":function(objArray){
			    	var _this=this;
			    	if(!this.grid){
			    		K.Tool.log("No gridset error.");
			    		return;
			    	}
			    	for(var i=objArray.length-1;i>=0;i--){
			    		var o=objArray[i],$tr=$("<tr />");
			    		if(this.grid.hasCheckbox){
			    			$tr.append("<td><div class='cb uncheck' /></td>");}
			    		for(var j=0;j<this.grid.cols.length;j++){
			    			var c=this.grid.cols[j],_txt=o[c.key]||"";
			    			var $td=$("<td>"+_txt+"</td>");
			    			$tr.append($td);
			    			if(c.link){
			    				$td.addClass("link");
			    				$td.click(function(_this,c){return function(){
		    						var o=$(this).closest("tr").data("o"),_evt;
		    						if(typeof(_this.events.onDataClick[c.key])=="function"){
				    					_evt=_this.events.onDataClick[c.key];
				    				}else{
				    					_evt=_this.events.onDataClick["_dft"];
				    				}
		    						_evt(_this,o);
		    					};}(_this,c));
			    			}
			    			if(i%2==0){$tr.addClass("tr-odd");}
			    		}
			    		$tr.data("o",o);
			    		this.$el.find(".gridtable>tbody").prepend($tr);
			    	}
			    	this.layout();
			    	this.activateCheckbox();
			    	return this;
			    },
			    "updateData":function(url_or_array){
			    	if(typeof(url_or_array)=="undefined"&&this.dataURL){
			    		url_or_array=this.dataURL;
			    	}else if(typeof(url_or_array)=="string"){
			    		if($.trim(url_or_array).length<=0&&this.dataURL){
			    			url_or_array=this.dataURL;
			    		}
			    		$.ajax({
				    		"url":url_or_array,
				    		"type":"get",
				    		"dataType":"json",
				    		"_this":this,
				    		"success":function(array){
				    			this._this.$el.find(".gridtable>tbody").empty();
				    			this._this.addData(array);
				    		},
				    		"error":function(e1,e2,e3){
				    			K.Tool.log(e2);
				    		}
				    	});
			    	}else{
			    		this.$el.find(".gridtable>tbody").empty();
			    		this.addData(url_or_array);
			    	}
			    	return this;
			    },
			    "layout":function(){
			    	var _this=this,$dom=this.$el.parent(),$basicgrid=this.$el;
			    	var oh=0,$grid=$basicgrid.find(".datatable-wrapper"),h1,h2;
	    			$basicgrid.width($dom.width()).height($dom.height());
	    			$grid.siblings().each(function(){oh+=$(this).height();});
	    			h1=$basicgrid.height()-oh;
	    			h2=$grid.find(".gridtable:last").height();
	    			$grid.height(h1);
	    			if(h1<=h2){
	    				$grid.css("overflow-y","scroll");
	    			}else{
	    				$grid.css("overflow-y","hidden");
	    			}
	    			return this;
			    },
			    "to":function($dom){
			    	if(this.grid){
			    		this.wrapper=$dom;
				    	this.$el.empty();
				    	this.events=$.extend({},this.events);
				    	if(this.grid.size&&typeof(this.grid.size)=="object"&&this.grid.size.length==1){
				    		this.grid.size=this.grid.size[0];
				    	}
				    	this.render(this.grid);
				    	$dom.append(this.$el);
				    	this.layout();
				    	$(window).resize(function(_this){
				    		return function(){
				    			_this.layout.call(_this);
				    		};
				    	}(this));
				    	this.activate();
			    	}else{
			    		K.Tool.log("No gridset error.");
			    	}
			    	return this;
			    },"activateCheckbox":function(){
			    	var _this=this,$all=this.$el.find(".cb[name='cb_all']");
		    		_this.$el.find(".gridtable").find(".cb").each(function(){
		    			$(this).unbind("click");
		    			$(this).click(function(){
		    				if($(this).hasClass("checked")){
				    			$(this).removeClass("checked").addClass("uncheck");
				    		}else{
				    			$(this).removeClass("uncheck").addClass("checked");
				    		}
		    			});
			    	});
		    		$all.click(function(_this){
		    			return function(){
		    				var ckd=$(this).hasClass("checked");
		    				_this.$el.find(".gridtable>tbody>tr").each(function(){
		    					$(this).find(".cb").removeClass("checked").removeClass("uncheck");
		    					if(ckd){
		    						$(this).find(".cb").addClass("checked");
		    					}
		    					else{$(this).find(".cb").addClass("uncheck");}
		    				});
		    			};
		    		}(_this));
		    	
			    },"activate":function(){
			    	var _this=this,$size=this.$el.find(".grid-size>.page-size"),$all=this.$el.find(".cb[name='cb_all']");
			    	this.$el.find(".buttons>.btn").each(function(){
			    		var $btn=$(this),_id=$btn.attr("data-id");
			    		$btn.unbind("click");
			    		$btn.click(function(_this,_id){
		    				return function(){
		    					var _evt,$btn=$(this);
		    					if(typeof(_this.events.onButtonClick[_id])=="function"){
					    			_evt=_this.events.onButtonClick[_id];
					    		}else{
					    			_evt=_this.events.onButtonClick["_dft"];
					    		}
		    					_evt(_this,$btn);
		    				};
		    			}(_this,_id));
			    	});
			    	if($size.length>0){
			    		$size.unbind("change");
			    		$size.change(function(_this){
			    			return function(){
			    				var val=_this.getSize();
			    				_this.events.onChageSize(_this,val);
			    			};
			    		}(_this));
			    	}
			    	if($all.length>0){
			    		_this.activateCheckbox();
			    	}
			    },"buttonClick":function(id,fn){
			    	this.events.onButtonClick[id]=function(){};
			    	if(typeof(fn)=="function"){this.events.onButtonClick[id]=fn;}
			    	return this;
			    },
			    "dataClick":function(key,fn){
			    	this.events.onDataClick[key]=function(){};
			    	if(typeof(fn)=="function"){this.events.onDataClick[key]=fn;}
			    	return this;
			    },
			    "changeSize":function(fn){
			    	if(typeof(fn)=="function"){this.events.onChageSize=fn;}
			    	return this;
			    },
			    "pageTo":function(fn){
			    	if(typeof(fn)=="function"){this.events.onPageTo=fn;}//HERE   pageTo实现
			    	return this;
			    },
			    "getSize":function(){
			    	if(this.$el.find(".grid-size>.page-size").length>0){
			    		return parseInt(this.$el.find(".grid-size>.page-size").children("option:selected").val());
			    	}else{
			    		return parseInt(this.grid.size||1);
			    	}
			    },
			    "getPageNumber":function(){
			    	if(this.$el.find(".pager-wrapper>span>.current").length>0){
			    		return parseInt(this.$el.find(".pager-wrapper>span>.current").text());
			    	}else{
			    		return 1;
			    	}
			    },
			    "getSelected":function(){
			    	var rst=[],$list=this.$el.find(".gridtable>tbody").find(".checked");
			    	$.each(function(){
			    		rst.push($(this).closest("tr").data("o"));
			    	});
			    	return rst;
			    },
			    "setPager":function(json){
			    	var setting={
							"size":20,
							"totalElements":1,
							"totalPages":1,
							"number":1,
							"prev":"Prev",
							"next":"Next",
							"_links":{
								"first":false,
								"last":false,
								"prev":false,
								"next":false
							}
			    	};
			    	this.pager=$.extend(setting,json);
			    	var _this=this,$wrapper=this.$el.find(".pager-wrapper"),$prev=$("<a class='pager-prev'>"),
			    		$next=$("<a class='pager-next'>"),$pages=$("<span />");
			    	$wrapper.empty();
			    	$wrapper.append($prev).append($pages).append($next);
			    	$prev.addClass("bg-c1").addClass("font-c4").text(_this.pager.prev);
			    	$next.addClass("bg-c1").addClass("font-c4").text(_this.pager.next);
			    	if(_this.pager.totalPages<10){
			    		for(var i=1;i<=_this.pager.totalPages;i++){
			    			var $a=$("<a class='bg-c1 font-c4' />");
			    			$a.text(i);
			    			$pages.append($a);
			    			if(_this.pager.number==i){
			    				$a.addClass("current");
			    			}
			    		}
			    	}else if(_this.pager.number<=5){
			    		for(var i=1;i<=5;i++){
			    			var $a=$("<a class='bg-c1 font-c4' />");
			    			$a.text(i);
			    			$pages.append($a);
			    			if(_this.pager.number==i){
			    				$a.addClass("current");
			    			}
			    		}
			    		$pages.append($("<i>…</i>"));
			    		$pages.append($("<a class='bg-c1 font-c4'>"+(_this.pager.totalPages-1)+"</a>"));
			    		$pages.append($("<a class='bg-c1 font-c4'>"+_this.pager.totalPages+"</a>"));
			    	}else if(_this.pager.number>=_this.pager.totalPages-5){
			    		$pages.append($("<a class='bg-c1 font-c4'>1</a>"));
			    		$pages.append($("<a class='bg-c1 font-c4'>2</a>"));
			    		$pages.append($("<i>…</i>"));
			    		for(var i=_this.pager.totalPages-5;i<=_this.pager.totalPages;i++){
			    			var $a=$("<a class='bg-c1 font-c4' />");
			    			$a.text(i);
			    			$pages.append($a);
			    			if(_this.pager.number==i){
			    				$a.addClass("current");
			    			}
			    		}
			    	}else{
			    		$pages.append($("<a class='bg-c1 font-c4'>1</a>"));
			    		$pages.append($("<i>…</i>"));
			    		for(var i=_this.pager.number-1;i<_this.pager.number+2;i++){
			    			var $a=$("<a class='bg-c1 font-c4' />");
			    			$a.text(i);
			    			$pages.append($a);
			    			if(_this.pager.number==i){
			    				$a.addClass("current");
			    			}
			    		}
			    		$pages.append($("<i>…</i>"));
			    		$pages.append($("<a class='bg-c1 font-c4'>"+_this.pager.totalPages+"</a>"));
			    	}
			    	if(parseInt(_this.$el.find(".current").text())==1){
			    		$prev.addClass("disable");
			    	}
	    			if(parseInt(_this.$el.find(".current").text())==parseInt(_this.$el.find(".pager-wrapper>span>a:last").text())){
	    				$next.addClass("disable");
	    			}
			    	$wrapper.find("a").each(function(){
			    		//onPageTo function(grid,page)
			    		var _page=parseInt($(this).text());
			    		if($(this).hasClass("pager-prev")){_page=parseInt(_this.$el.find(".current").text())-1;}
			    		if($(this).hasClass("pager-next")){_page=parseInt(_this.$el.find(".current").text())+1;}
			    		$(this).click(function(_this,_page){return function(){
			    			if($(this).hasClass("current")||$(this).hasClass("disable"))return;
			    			_this.events.onPageTo(_this,_page);
			    		};}(_this,_page));
			    	});
			    	this.layout();
			    	return this;
			    },
			    "render":function(json){  
			        this.$el.html(this.template(json));
			        return this.$el;
			     }
			});
		},"init":function($dom){
			$dom.empty();
			var grid=new K.BasicGrid.View();
			grid.to($dom);
			return grid;
		}	
	}
});
$.extend(_jstemplates,{"BasicGrid":function(cb){
	if($("#_template").length<=0){
		$("body").append($("<div type='text/template' id='_template' />"));
	}
	var arr=[];
	arr.push('<script id="t_basicgrid" type="text/template">');
	arr.push('<h3 class="grid-title font-c3">{-title}</h3>');
	arr.push('<p class="grid-subtitle">{-subtitle}</p>');
	arr.push('<div class="buttons">');
	arr.push('{for(var i=0;i<buttons.length;i++){var btn=buttons[i];}');
	arr.push('<div class="btn bg-c1 font-c4" data-id="{-btn.id}">{-btn.text}</div>');
	arr.push('{}}');
	arr.push('<div style="clear:both;"></div>');
	arr.push('</div>');
	arr.push('<div class="grid_blank_4"></div>');
	arr.push('<div class="grid-size">');
	arr.push('{if(size&&typeof(size)=="object"&&size.length>1){}');
	arr.push('<a class="page-size-text">{-sizeText}</a><select class="page-size">');
	arr.push('{for(var i=0;i<size.length;i++){}');
	arr.push('<option value="{-size[i]}">{-size[i]}</option>');
	arr.push('{}}');
	arr.push('</select>');
	arr.push('{}}');
	arr.push('</div>');
	arr.push('<div class="grid_blank_4"></div>');
	arr.push('<div class="datatable-wrapper">');
	arr.push('<table class="gridtable" style="width:100%">');
	arr.push('<thead><tr class="font-c2">');
	arr.push('{if(hasCheckbox){}');
	arr.push('<th style="width:32px;"><div class="cb uncheck" name="cb_all" /></th>');
	arr.push('{}}');
	arr.push('{for(var i=0;i<cols.length;i++){var col=cols[i];}');
	arr.push('<th data-key="{-col.key}" style="width:{-col.w}%">{-col.text}</th>');
	arr.push('{}}');
	arr.push('</tr></thead>');
	arr.push('<tbody>');
	arr.push('</tbody>');
	arr.push('</table>');
	arr.push('</div>');
	arr.push('<div class="grid_blank_4"></div>');
	arr.push('<div class="pager-wrapper">');
	arr.push('</div>');
	arr.push('</script>');
	if($("#_template").find("#t_basicgrid").length<=0)
	$("#_template").append(arr.join(""));
	if(typeof(cb)=="function")cb();
}});